package com.example.demo.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MuiscUtil {

    private static ObjectMapper objectMapper = new ObjectMapper();


    //通过条件搜索音乐
    public static List<Object> getMp3(String name,Integer page) {
        String url = "http://msearchcdn.kugou.com/api/v3/search/song";
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("showtype","14");
        stringStringHashMap.put("pagesize",20);
        stringStringHashMap.put("tag_aggr",1);
        stringStringHashMap.put("tagtype","全部");
        stringStringHashMap.put("plat",0);
        stringStringHashMap.put("sver",5);
        stringStringHashMap.put("keyword",name);
        stringStringHashMap.put("correct",1);
        stringStringHashMap.put("api_ver",1);
        stringStringHashMap.put("version",9108);
        stringStringHashMap.put("page",page);
        stringStringHashMap.put("area_code",1);
        stringStringHashMap.put("tag",1);
        stringStringHashMap.put("with_res_tag",1);
        String get = MyNetworkUtil.getParam("GET", url, stringStringHashMap);
        System.out.println(get);
        get =get.substring(get.indexOf(">")+1,get.lastIndexOf("<"));
        System.out.println(get);
        Map map = null;
        try {
            map = objectMapper.readValue(get, Map.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return (List<Object>)((Map<String,Object>) map.get("data")).get("info");
    }


    //根据音乐信息拿到音乐详细信息
    public static Map<String,Object> getUrl(String hash,String album_id){
        String url = "https://wwwapi.kugou.com/yy/index.php";
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("r","play/getdata");
        stringStringHashMap.put("hash",hash);
        stringStringHashMap.put("album_id",album_id);
        stringStringHashMap.put("dfid","3w06XG1E0au711AILd3zMxED");
        stringStringHashMap.put("mid","3970cebc37011d468f3d9cf0bccc2366");
        stringStringHashMap.put("platid","4");
        stringStringHashMap.put("_",String.valueOf(System.currentTimeMillis()));
        String get = MyNetworkUtil.getParam("GET", url, stringStringHashMap);
        Map map = null;
        try {
            map = objectMapper.readValue(get, Map.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return (Map<String,Object>)map;
    }

}
